- drawing function plots in Xi -
We have already seen a simple example for the 'plot' command at the beginning of this example book. Now let's do it again a little more detailed. Say you simply want to plot the sinus function in the interval from -10 to 10. First of all you have to subdive this interval into a suitable number of tiles, say 500. For this purpose Xi has implemented the interval library function:
( 1)>x=interval(-10,10,500);Now the interval [-10,10] is subdivided into 500 tiles and the x coordinates are stored in a new array 'x'. You can use this array as a input for the sinus function:
( 2)>y=sin(x);With this command you calculate the sinus for each element of the 'x' array and store the result in a new array 'y'. Now you are able to plot the sinus function:
( 3)>plot(x,y);Hmm..., the 'plot' command hasn't done more than plotting a single dot at each (x,y) coordinate. Okay, one could recognize the sinus function but this is not quite the result you want. Lets connect the points with lines:
( 4)>plot(x,y,\line);Looks a lot better. Hmm..., but look at the axes - Xi sets the x/y ranges as small as possible to show the whole plot. In this case the x-axis goes from -10 to 10 but the y-axis not exactly from -1 to 1 because none of the y-values is exactly 1 or -1 (the calculation of the sinus itself has some numerical errors). However we want the y-axis to begin at -1 and end at 1:
( 5)>plot(\yrange={-1,1});That's better. But we can still make some improvements. E.g. we can put some titles to the axes:
( 6)>plot(\xtitle="x",\ytitle="y");And finally a title for the whole plot:
( 7)>plot(\title="The Sinus function");Okay that's it or could you think of further improvements? If yes mail us. Now we want to compare the sinus function with the cosinus function. Of course you could simply add a cosinus to the existing plot but we want a second plot above the first. Therefore we have to reposition the plot:
( 8)>plot(\position={0,0,1,0.5});This positions the plot to the lower half of the window. The values of the \position arguments are given in normal coordinate, i.e. (0,0) is the lower left corner of the plot device and (1,1) is the upper right corner of the plot device. Now we calculate the y-coordinates of the Cosinus:
( 9)>y=cos(x);This will erase the previous calculated y-values of the Sinus function that we don't need any more. Now we open a new plot with the Cosinus inside:
( 10)>plot(x,y,\line,\num=1,\linestyle={5,3});The \num argument sets the number of the actual plot (the counting starts at 0, hence the above plot of the Sinus is number 0).The parameter \linestyle determines the style of the line. Here the result is a dotted line where 5 plotted points alternate with 3 unplotted points. However this new plot intersects with the other plot - that's not good. Let's move it to the upper half
( 11)>plot(\position={0,0.5,1,1});and decorate it in the same way as the Sinus plot:
( 12)>plot(\yrange={-1,1}); ( 13)>plot(\title="The Cosinus function",\xtitle="x",\ytitle="y");That's it. Now for the mouse options. With the left mouse button you can select one of the two plots. The Properties Item in the Edit menu will then become accessable. Keep the left mouse button pressed to drag the image with the mouse. With the right mouse button you can zoom inside each plot. Try it and remember that there is an Undo Button for undoing unwanted changes.
Lets plot a histogram of 10000 normal distributed random numbers.
( 14)>set_seed(1234); ( 15)>rn=randomn(10000); ( 16)>[h,x]=histogram(rn,\binsize=0.1); ( 17)>plot(x,h,\hist);The argument \hist<\I> sets the linestyle to the histogram-style.